/*
 */

package labeledbox;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 */
public class AnimationControls extends JPanel
                               implements ControlPanel_Observer_Interface, 
                                          ActionListener
                                         /* ChangeListener */  {

  DrawPanel drawPanel;  // like a model for this app

  JLabel panel_label   = new JLabel("Move To:");
  JTextField leftField = new JTextField(4);
  JLabel     leftLabel = new JLabel("Left: ");
  JTextField topField  = new JTextField(4);
  JLabel     topLabel  = new JLabel("Top: ");

  JSlider  slider      = new JSlider(1, 10);
  JLabel   sliderLabel = new JLabel("Length of Animation");

  JButton performBtn   = new JButton("Perform Animation");

  private final int FRAMES_PER_SECOND = 25;

  LabeledBox selectedLabeledBox;  // store so actionPerformed() can get access
  ///// end class vars ////
  /////////////////////////

  // Constructor
  public AnimationControls(DrawPanel drawPanel) {
    
    this.drawPanel = drawPanel;
    this.drawPanel.addObserver(this);

    leftField.setEnabled( false);
    topField.setEnabled(  false);
    performBtn.setEnabled(false);
    slider.setEnabled(    false);

    //Turn on labels at major tick marks.
    slider.setMajorTickSpacing(1);
    slider.setMinorTickSpacing(1);
    slider.setPaintTicks(true);
    slider.setPaintLabels(true);
        //    // Setup EVENT handler for slider
        //    slider.addChangeListener(this);
    // Register event handler for the "Perform Animation" button
    performBtn.addActionListener(this);

    panel_label.setFont(new Font("sans-serif", Font.BOLD, 20));

    layout_panel();
  } //// end constructor ///

  public void notifyBoxSelected(LabeledBox selectedLabeledBox) {

    this.selectedLabeledBox = selectedLabeledBox;
    // If there IS a selected box
    if(selectedLabeledBox != null ) {
      // Enable the widgets
      leftField.setEnabled(true);
      topField.setEnabled( true);
      performBtn.setEnabled(true);
      slider.setEnabled(    true);
    }
    else {
      // Disable widgets  if no box was selected
      leftField.setEnabled( false);
      topField.setEnabled(  false);
      performBtn.setEnabled(false);
      slider.setEnabled(    false);
    }

  } /////// end notifyBoxSelected() ////////

  private void layout_panel() {

    leftField.setMaximumSize(new Dimension(45, 25));
    topField.setMaximumSize(new Dimension(45, 25));

    Box outerBox = Box.createVerticalBox();
    outerBox.add(Box.createRigidArea(new Dimension(0, 20)));
    outerBox.add(panel_label);
    outerBox.add(Box.createRigidArea(new Dimension(0, 20)));

    Box leftBox = Box.createHorizontalBox();
    leftBox.add(leftLabel);
    leftBox.add(leftField);
    outerBox.add(leftBox);
    outerBox.add(Box.createRigidArea(new Dimension(0, 10)));

    Box topBox = Box.createHorizontalBox();
    topBox.add(topLabel);
    topBox.add(topField);
    outerBox.add(topBox);
    outerBox.add(Box.createRigidArea(new Dimension(0, 10)));

    outerBox.add(slider);
    outerBox.add(Box.createRigidArea(new Dimension(0, 8)));
    outerBox.add(sliderLabel);
    outerBox.add(Box.createRigidArea(new Dimension(0, 20)));

    outerBox.add(performBtn);

    outerBox.add(Box.createRigidArea(new Dimension(0, 20)));
    add(outerBox);
  } ///// end layout_panel() ////////


  // Event handler for the "Perform Animation" Button
  public void actionPerformed(ActionEvent e) {

    int left = Integer.parseInt(leftField.getText());
    int top  = Integer.parseInt(topField.getText());
    int length = slider.getValue();
    int tot_animation_frames = length * FRAMES_PER_SECOND;
    
    // System.err.println("You entered left:" + left + ", top:" + top + ", length:" + length);

    AnimateBox animator = new AnimateBox(selectedLabeledBox, left, top, tot_animation_frames, drawPanel);

    // Get a TIMER for the animation.
    // first arg is interval between firings (in miliseconds)
    javax.swing.Timer timer = new javax.swing.Timer(1000 / FRAMES_PER_SECOND, (ActionListener) animator);

    animator.setTimer(timer);
    timer.start();

  } /////// end actionPerformed() ///////

} //////////////////// end class AnimationControls /////////////////////////////
